
import search from "../../../../template/search/index";
import api from '../../../../services/api'
var app = getApp();

Page({
  data: {
    indexList: [],
    brandGroupList: [],
    filterList: null,
    /**选择车辆型号开始 */
    carBrand: {},
    carSeries: {},
    seriesData: {},
    showCarPopup: false,
    brandList: [],
    currentSeriesList: [],
    /**选择车辆型号结束 */
    keyWord: '',
    type: '',
    seriesName: '',
    productName: '',
    productId: 0,
    onlySelectBrand: '0'
  },
  onLoad: function (e) {
    if (e.type) {
      console.log('e.productId', e.productId)
      this.setData({ 
        type: e.type,
        qd: e.qd,
        productId: e.productId,
        productName: e.productName,
        seriesName: e.seriesName
      });
    }
    if (e.onlySelectBrand) {
      this.setData({ 
        onlySelectBrand: e.onlySelectBrand
      });
    }
    search.init(this);
  },
  onShow() {
    this.queryBrandList();
  },


  onSearch() {
    const { keyWord, brandGroupList } = this.data;
    if (keyWord) {
      let filterList = [];
      let newIndexList = [];
      for (let i = 0; i < brandGroupList.length; i++) {
        const brand = brandGroupList[i];
        let newBrandList = [];
        for (let j = 0; j < brand.brandList.length; j++) {
          if (brand.brandList[j].brandName.includes(keyWord)) {
            newBrandList.push(brand.brandList[j]);
          }
          if (newBrandList.length > 0) {
            brand.brandList = newBrandList;
            filterList.push(brand);
            newIndexList.push(brand.groupName);
          }
        }
      }
      if (filterList.length > 0) {
        this.setData({ filterList, indexList: newIndexList, showCarPopup: false });
      } else {
        wx.showToast({
          title: "未搜索到相关品牌，请重新输入",
          icon: "none",
          duration: 1500
        })
        return;
      }

    } else {
      this.setData({ filterList: null, indexList: brandGroupList.map(item => item.groupName), showCarPopup: false });
    }


  },

  /***选择车型 开始****/
  openCarPopup(e) {
    if (this.data.onlySelectBrand === '1') {
      const { brandList } = this.data;
      const brandId = e.currentTarget.dataset.id;
      const carBrand = brandList.find(item => item.brandId === brandId) || {};
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];
      prevPage.setData({
        carBrandName: carBrand.brandName
      }, function() {
        wx.navigateBack({
          delta: 1 
        })
      });
    } else {
      const { brandList, seriesData } = this.data;
      const brandId = e.currentTarget.dataset.id;
      const carBrand = brandList.find(item => item.brandId === brandId) || {};
      if (seriesData[brandId]) {
        this.setData({ showCarPopup: true, carBrand, currentSeriesList: seriesData[brandId] });
      } else {
        this.setData({
          showCarPopup: true, carBrand
        });
        this.querySeriesList().then(data => {
        });
      }
    }
  },
  onCarCancel() {
    this.setData({ showCarPopup: false });
  },
  async queryBrandList() {
    const res = await api.quote.queryBrandGroupList();
    if (res.status == 200 && res.data && res.data.length > 0) {
      let brandList = [], indexList = [];
      for (let i = 0; i < res.data.length; i++) {
        indexList.push(res.data[i].groupName);
        for (let j = 0; j < res.data[i].brandList.length; j++) {
          brandList.push(res.data[i].brandList[j]);
        }
      }
      this.setData({ brandGroupList: res.data, brandList, indexList });
    }
  },
  async querySeriesList() {
    const { carBrand, seriesData } = this.data;
    const res = await api.quote.queryCarModelList({ brandId: carBrand.brandId });
    if (res.status == 200) {
      seriesData[carBrand.brandId] = res.data;
      this.setData({ seriesData, currentSeriesList: res.data });
    }
    return res.data || [];
  },

  /***选择车型 结束****/
  toCostQuery(e) {
    const { currentSeriesList, type, qd, productId, productName, seriesName  } = this.data;
    const id = e.currentTarget.id
    const carSeries = currentSeriesList.find(item => item.id === id);
    if (type) {
      wx.navigateTo({ 
        url: `/pages/index/cost/result/index?type=${type}&qd=${qd}&productId=${productId}&productName=${productName}&seriesName=${seriesName}&carSeriesId=${carSeries.id}&carModel=${carSeries.seriesName}`
      });
    } else {
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];
      let _that = this
      prevPage.setData({
        carBrandId: _that.data.carBrand.brandId,
        carBrandName: _that.data.carBrand.brandName,
        carSeriesId: carSeries.id,
        carSeriesName: carSeries.seriesName
      }, function() {
        wx.navigateBack({
          delta: 1 
        })
      });
    }
  }
});
